package yangsw87.swing;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.LayoutManager;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

import java.awt.Toolkit;
import java.awt.Dimension;

import javax.swing.JLabel;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;

import java.awt.CardLayout;

import javax.swing.LayoutStyle.ComponentPlacement;

import java.awt.Color;
import java.awt.Font;

import javax.swing.JTabbedPane;
import javax.swing.JRadioButtonMenuItem;
import javax.swing.JRadioButton;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class AComp extends JDialog {

	private final JPanel contentPanel = new JPanel();
	private JTextField txtUid;
	private JTextField txtPwd;
	private JLabel myNick;
	private JTextField textField;
	private final CardLayout  cards = new CardLayout(0,0);//cardlayout을 따로 지정하자.
	
	private JPanel myPane = new JPanel();
	private JTabbedPane mainPane = new JTabbedPane(JTabbedPane.TOP);
	
	
	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		try {
			AComp dialog = new AComp();
			dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
			dialog.setVisible(true);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Create the dialog.
	 */
	public AComp() {
		setIconImage(Toolkit.getDefaultToolkit().getImage(AComp.class.getResource("/yangsw87/swing/images/icon/icon.png")));
		setBounds(100, 100, 640, 480);
		setLocationRelativeTo(null);
		setTitle("고급 컴퍼넌트");
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		contentPanel.setLayout(new BorderLayout(0, 0));
		{
			JPanel top = new JPanel();
			top.setPreferredSize(new Dimension(10, 120));
			contentPanel.add(top, BorderLayout.NORTH);
			top.setLayout(new BorderLayout(0, 0));
			{
				JLabel topLogo = new JLabel("-");
				topLogo.setPreferredSize(new Dimension(400, 15));
				topLogo.setIcon(new ImageIcon(AComp.class.getResource("/yangsw87/swing/images/background/top.jpg")));
				top.add(topLogo, BorderLayout.WEST);
			}
			{
				final JPanel cardPane = new JPanel();
				cardPane.setPreferredSize(new Dimension(220, 10));
				top.add(cardPane, BorderLayout.EAST);
				cardPane.setLayout(cards);//new CardLayout(0,0)를 위에서 변수로 선언!!! cards로 변경
				{
					JPanel loginPane = new JPanel();
					cardPane.add(loginPane, "login");
					{
						txtUid = new JTextField();
						txtUid.setColumns(10);
					}
					txtPwd = new JTextField();
					txtPwd.setColumns(10);
					
					JButton loginBtn = new JButton("로그인");
/*					loginBtn.addActionListener(new ActionListener() {
						public void actionPerformed(ActionEvent e) {
							cards.show(cardPane,"logout");//로그인 하면 로그아웃 창으로 변경
							//cards:카드레이아웃
							//cardPane:카드레이아웃이 적용된 패널
							//logout:cardPane에 보여줄 패널이름
						}
					});*/
					
					loginBtn.addActionListener(new ActionListener() {
						public void actionPerformed(ActionEvent e) {
							String muid = txtUid.getText();//txtUid에 저장된 text 값을 불러온다.
							String mpwd = txtPwd.getText();
							
							if(muid.equals("")&& !mpwd.equals("")){
								JOptionPane.showMessageDialog(null, "아이디를 입력하세요");
							}
							else if(muid.equals("") && mpwd.equals("")){
								JOptionPane.showMessageDialog(null,	"아이디와 비번을 입력하세요");
							}
							else if(mpwd.equals("")&& !muid.equals("")){
								JOptionPane.showMessageDialog(null, "비번 입력하세요");
							}
							else if(muid.equals("abc") && mpwd.equals("12345")){
								JOptionPane.showMessageDialog(null, "접속완료");
								cards.show(cardPane, "logout");
								//myPane.setEnabled(false);
								mainPane.setEnabledAt(2,true);//괄호 안에 숫자의 의미는 몇번째 탭인지를 표시 0번부터 시작
								//탭활성/비활성은
								//setEnabledAt을 이용
							}
							else{
								JOptionPane.showMessageDialog(null, "다시생각해봐");
							}							
						}
					});
									
					
					JButton joinBtn = new JButton("회원가입");

					GroupLayout gl_loginPane = new GroupLayout(loginPane);
					gl_loginPane.setHorizontalGroup(
						gl_loginPane.createParallelGroup(Alignment.LEADING)
							.addGroup(gl_loginPane.createSequentialGroup()
								.addContainerGap()
								.addGroup(gl_loginPane.createParallelGroup(Alignment.LEADING, false)
									.addComponent(txtUid, 0, 0, Short.MAX_VALUE)
									.addComponent(txtPwd, GroupLayout.DEFAULT_SIZE, 110, Short.MAX_VALUE))
								.addPreferredGap(ComponentPlacement.RELATED)
								.addGroup(gl_loginPane.createParallelGroup(Alignment.LEADING)
									.addComponent(joinBtn, GroupLayout.DEFAULT_SIZE, 91, Short.MAX_VALUE)
									.addComponent(loginBtn, GroupLayout.DEFAULT_SIZE, 91, Short.MAX_VALUE)))
					);
					gl_loginPane.setVerticalGroup(
						gl_loginPane.createParallelGroup(Alignment.LEADING)
							.addGroup(gl_loginPane.createSequentialGroup()
								.addGap(28)
								.addGroup(gl_loginPane.createParallelGroup(Alignment.BASELINE)
									.addComponent(txtUid, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
									.addComponent(loginBtn))
								.addGap(18)
								.addGroup(gl_loginPane.createParallelGroup(Alignment.BASELINE)
									.addComponent(txtPwd, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
									.addComponent(joinBtn))
								.addContainerGap(28, Short.MAX_VALUE))
					);
					loginPane.setLayout(gl_loginPane);
				}
				{
					JPanel logoutPane = new JPanel();
					cardPane.add(logoutPane, "logout");
					{
						myNick = new JLabel("천송이");
						myNick.setFont(new Font("굴림", Font.BOLD, 12));
					}
					JButton logoutBtn = new JButton("로그아웃");
					logoutBtn.addActionListener(new ActionListener() {
						public void actionPerformed(ActionEvent e) {
							cards.show(cardPane, "login");
							mainPane.setEnabledAt(2, false);
						}
					});
					
					JLabel myAvatar = new JLabel("");
					myAvatar.setIcon(new ImageIcon(AComp.class.getResource("/yangsw87/swing/images/icon/icon2.png")));
					GroupLayout gl_logoutPane = new GroupLayout(logoutPane);
					gl_logoutPane.setHorizontalGroup(
						gl_logoutPane.createParallelGroup(Alignment.TRAILING)
							.addGroup(gl_logoutPane.createSequentialGroup()
								.addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
								.addComponent(myAvatar)
								.addGroup(gl_logoutPane.createParallelGroup(Alignment.LEADING)
									.addGroup(gl_logoutPane.createSequentialGroup()
										.addGap(18)
										.addComponent(logoutBtn))
									.addGroup(gl_logoutPane.createSequentialGroup()
										.addGap(38)
										.addComponent(myNick)))
								.addGap(61))
					);
					gl_logoutPane.setVerticalGroup(
						gl_logoutPane.createParallelGroup(Alignment.LEADING)
							.addGroup(Alignment.TRAILING, gl_logoutPane.createSequentialGroup()
								.addContainerGap(32, Short.MAX_VALUE)
								.addGroup(gl_logoutPane.createParallelGroup(Alignment.LEADING)
									.addGroup(Alignment.TRAILING, gl_logoutPane.createSequentialGroup()
										.addComponent(myNick)
										.addGap(18)
										.addComponent(logoutBtn))
									.addComponent(myAvatar))
								.addGap(24))
					);
					logoutPane.setLayout(gl_logoutPane);
				}
			}
		}
		{
			//JTabbedPane mainPane = new JTabbedPane(JTabbedPane.TOP);
			contentPanel.add(mainPane, BorderLayout.CENTER);
			{
				JPanel basicFind = new JPanel();
				mainPane.addTab("기본검색", new ImageIcon(AComp.class.getResource("/yangsw87/swing/images/menu/new.png")), basicFind, null);
				{
					JButton btnNewButton = new JButton("New button");
					basicFind.add(btnNewButton);
				}
			}
			{
				JPanel detailFind = new JPanel();
				mainPane.addTab("상세검색", new ImageIcon(AComp.class.getResource("/yangsw87/swing/images/menu/exit.png")), detailFind, null);
				{
					textField = new JTextField();
					detailFind.add(textField);
					textField.setColumns(10);
				}
			}
			{
				//JPanel myPane = new JPanel();
				mainPane.addTab("마이페이지", new ImageIcon(AComp.class.getResource("/yangsw87/swing/images/menu/close.png")), myPane, null);
				mainPane.setEnabledAt(2, false);
				{
					JRadioButton rdbtnNewRadioButton = new JRadioButton("New radio button");
					myPane.add(rdbtnNewRadioButton);
				}
			}
		}
		{
			JPanel buttonPane = new JPanel();
			buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
			getContentPane().add(buttonPane, BorderLayout.SOUTH);
			{
				JButton okButton = new JButton("OK");
				okButton.setActionCommand("OK");
				buttonPane.add(okButton);
				getRootPane().setDefaultButton(okButton);
			}
			{
				JButton cancelButton = new JButton("Cancel");
				cancelButton.setActionCommand("Cancel");
				buttonPane.add(cancelButton);
			}
		}
	}
}
